<?php
/**
 * Created by PhpStorm.
 * User: 秋枫雁飞
 * Date: 2019/3/28
 * Time: 10:59
 */

namespace Aiweline\Core\Helper;

class Request
{
    private $_request;

    /**
     * [__construct 构造函数]
     * @param \Magento\Framework\Webapi\Rest\Request $request [请求]
     */
    function __construct(
        \Magento\Framework\Webapi\Rest\Request $request
    )
    {
        $this->_request = $request;
    }

    /**
     * 获取请求
     *
     * @return \Magento\Framework\App\RequestInterface
     */
    public function getRequest()
    {
        return $this->_request;
    }

    /**
     * Fetch and return parameter data from the request.[重写getRequestData]
     *
     * @return array
     */
    public function getRequestData()
    {
        $requestBodyParams = [];
        $params = $this->getRequest()->getParams();

        $httpMethod = $this->getRequest()->getHttpMethod();
        if ($httpMethod == 'POST' ||
            $httpMethod == 'PUT'
        ) {
            $requestBodyParams = $this->getRequest()->getBodyParams();
        }
        if ($httpMethod == 'DELETE'
        ) {
            //$requestBodyParams = array('delete', $this->getRequest()->getBodyParams());
            /*
             * I get a array like this ['delete'=>['key'=>value]]
             * */
            $requestBodyParams = $this->getRequest()->getBodyParams();
            /*
             * I get a array like this [value]
             * */
        }

        return array_merge($requestBodyParams, $params);
    }
}